#coding=utf-8

import Queue
import threading
import requests
import re
class trd(threading.Thread):
    def __init__(self,queue,spider):
        threading.Thread.__init__(self)
        self.queue=queue
        self.spider=spider
    def run(self):
        while not self.queue.empty():
            url=self.queue.get()
            self.spider(url)

def put_url_list(url_list,spider,thread_count=5):
    threads=[]
    que=Queue.Queue()
    for i in url_list:
        que.put(i)
    for i in range(thread_count):
        threads.append(trd(que,spider))
    for i in range(thread_count):
        threads[i].start()
    for i in range(thread_count):
        threads[i].join()

#上面方法，下面实现方法
def spider(url):
    r = requests.get(url)
    courses = re.findall(r'<h3 class="course-card-name">(.*?)</h3>', r.content.decode("utf-8"))
    for i in courses:
        print i

def url():
    url="https://www.imooc.com/course/list?page="
    pagenum=35
    thread_count=6
    url_list=[]
    for i in range(1,pagenum):
        url_list.append(url+str(i))
    #外部调用时Mult_que_temp.put_url_list(url_list=url_list,spider=spider,thread_count=thread_count)
    put_url_list(url_list=url_list,spider=spider,thread_count=thread_count)
if __name__=="__main__":
    url()